package com.jh.paymentchannel.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

/**
 * POS预支付、条码支付 交易流水表
 */
@Entity
@Table(name = "t_congming_order")
public class CongMingOrder implements Serializable {
    private static final long serialVersionUID = 4668981380841269891L;

    @Id
    @Column(name = "id")
    private long id;

    /**
     * userId
     */
    @Column(name = "user_id")
    private long userId;

    /**
     * 开店管家商户id
     */
    @Column(name = "shop_id")
    private String shopId;

    /**
     * 预支付：支付状态：支付成功SUCCESS，支付失败FAIL
     */
    @Column(name = "result_code")
    private String resultCode;

    /**
     * 支付失败原因
     */
    @Column(name = "result_msg")
    private String resultMsg;

    /**
     * 快银商户id，有注册完成后的真实id经过MD5生成
     */
    @Column(name = "congming_shop_id")
    private String congMingShopId;

    /**
     * 预支付：交易金额，保留2位小数
     */
    @Column(name = "money")
    private BigDecimal money = BigDecimal.ZERO;

    /**
     * 预支付：支付类型：微信weixin，支付宝alipay
     */
    @Column(name = "type")
    private String type;

    /**
     * 支付成功时间：yyyyMMddHHmmss
     */
    @Column(name = "time_end")
    private String timeEnd;

    /**
     * 订单号
     */
    @Column(name = "order_id")
    private String orderId;

    /**
     * 快银订单号
     */
    @Column(name = "out_trade_no")
    private String outTradeNo;

    /**
     * 加密sign
     */
    @Column(name = "sign")
    private String sign;

    /**
     * 条码扫码：返回状态：SUCCESS成功，FAIL失败
     */
    @Column(name = "return_code")
    private String returnCode;

    /**
     * 条码扫码：支付状态：SUCCESS支付成功，FAIL支付失败，PAYING支付中；客户端超时未返回状态，需调用查询接口查看订单状态
     */
    @Column(name = "pay_status")
    private String payStatus;

    /**
     * 异常说明信息
     */
    @Column(name = "pay_msg")
    private String payMsg;

    /**
     * 条码扫码：订单类型：微信weixin，支付宝alipay
     */
    @Column(name = "order_type")
    private String orderType;

    /**
     * 通道流水号，退款单号
     */
    @Column(name = "transaction_id")
    private String transactionId;

    /**
     * 条码扫码：用户应付金额
     */
    @Column(name = "pay_able")
    private BigDecimal payable = BigDecimal.ZERO;

    /**
     * 活动优惠金额
     */
    @Column(name = "activity")
    private BigDecimal activity = BigDecimal.ZERO;

    /**
     * 条码扫码：用户实付金额
     */
    @Column(name = "paid_out")
    private BigDecimal paidOut = BigDecimal.ZERO;

    /**
     * 手续费金额
     */
    @Column(name = "commission")
    private BigDecimal commissin = BigDecimal.ZERO;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public String getShopId() {
        return shopId;
    }

    public void setShopId(String shopId) {
        this.shopId = shopId;
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    public String getCongMingShopId() {
        return congMingShopId;
    }

    public void setCongMingShopId(String congMingShopId) {
        this.congMingShopId = congMingShopId;
    }

    public BigDecimal getMoney() {
        return money;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getTimeEnd() {
        return timeEnd;
    }

    public void setTimeEnd(String timeEnd) {
        this.timeEnd = timeEnd;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getOutTradeNo() {
        return outTradeNo;
    }

    public void setOutTradeNo(String outTradeNo) {
        this.outTradeNo = outTradeNo;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getReturnCode() {
        return returnCode;
    }

    public void setReturnCode(String returnCode) {
        this.returnCode = returnCode;
    }

    public String getPayStatus() {
        return payStatus;
    }

    public void setPayStatus(String payStatus) {
        this.payStatus = payStatus;
    }

    public String getPayMsg() {
        return payMsg;
    }

    public void setPayMsg(String payMsg) {
        this.payMsg = payMsg;
    }

    public String getOrderType() {
        return orderType;
    }

    public void setOrderType(String orderType) {
        this.orderType = orderType;
    }

    public String getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(String transactionId) {
        this.transactionId = transactionId;
    }

    public BigDecimal getPayable() {
        return payable;
    }

    public void setPayable(BigDecimal payable) {
        this.payable = payable;
    }

    public BigDecimal getActivity() {
        return activity;
    }

    public void setActivity(BigDecimal activity) {
        this.activity = activity;
    }

    public BigDecimal getPaidOut() {
        return paidOut;
    }

    public void setPaidOut(BigDecimal paidOut) {
        this.paidOut = paidOut;
    }

    public BigDecimal getCommissin() {
        return commissin;
    }

    public void setCommissin(BigDecimal commissin) {
        this.commissin = commissin;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CongMingOrder)) return false;
        CongMingOrder that = (CongMingOrder) o;
        return getId() == that.getId() &&
                getUserId() == that.getUserId() &&
                Objects.equals(getShopId(), that.getShopId()) &&
                Objects.equals(getResultCode(), that.getResultCode()) &&
                Objects.equals(getResultMsg(), that.getResultMsg()) &&
                Objects.equals(getCongMingShopId(), that.getCongMingShopId()) &&
                Objects.equals(getMoney(), that.getMoney()) &&
                Objects.equals(getType(), that.getType()) &&
                Objects.equals(getTimeEnd(), that.getTimeEnd()) &&
                Objects.equals(getOrderId(), that.getOrderId()) &&
                Objects.equals(getOutTradeNo(), that.getOutTradeNo()) &&
                Objects.equals(getSign(), that.getSign()) &&
                Objects.equals(getReturnCode(), that.getReturnCode()) &&
                Objects.equals(getPayStatus(), that.getPayStatus()) &&
                Objects.equals(getPayMsg(), that.getPayMsg()) &&
                Objects.equals(getOrderType(), that.getOrderType()) &&
                Objects.equals(getTransactionId(), that.getTransactionId()) &&
                Objects.equals(getPayable(), that.getPayable()) &&
                Objects.equals(getActivity(), that.getActivity()) &&
                Objects.equals(getPaidOut(), that.getPaidOut()) &&
                Objects.equals(getCommissin(), that.getCommissin()) &&
                Objects.equals(getCreateTime(), that.getCreateTime());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getId(), getUserId(), getShopId(), getResultCode(), getResultMsg(), getCongMingShopId(), getMoney(), getType(), getTimeEnd(), getOrderId(), getOutTradeNo(), getSign(), getReturnCode(), getPayStatus(), getPayMsg(), getOrderType(), getTransactionId(), getPayable(), getActivity(), getPaidOut(), getCommissin(), getCreateTime());
    }

    @Override
    public String toString() {
        return "CongMingOrder{" +
                "id=" + id +
                ", userId=" + userId +
                ", shopId='" + shopId + '\'' +
                ", resultCode='" + resultCode + '\'' +
                ", resultMsg='" + resultMsg + '\'' +
                ", congMingShopId='" + congMingShopId + '\'' +
                ", money=" + money +
                ", type='" + type + '\'' +
                ", timeEnd='" + timeEnd + '\'' +
                ", orderId='" + orderId + '\'' +
                ", outTradeNo='" + outTradeNo + '\'' +
                ", sign='" + sign + '\'' +
                ", returnCode='" + returnCode + '\'' +
                ", payStatus='" + payStatus + '\'' +
                ", payMsg='" + payMsg + '\'' +
                ", orderType='" + orderType + '\'' +
                ", transactionId='" + transactionId + '\'' +
                ", payable=" + payable +
                ", activity=" + activity +
                ", paidOut=" + paidOut +
                ", commissin=" + commissin +
                ", createTime=" + createTime +
                '}';
    }

    public void addCongMingOrder(MicroPay microPay) {
        setReturnCode(microPay.getReturn_code());
        setPayStatus(microPay.getPay_status());
        setOrderId(microPay.getOrderId());
        setOutTradeNo(microPay.getOut_trade_no());
        setOrderType(microPay.getOrder_type());
        setTimeEnd(microPay.getTimeEnd());
        setTransactionId(microPay.getTransaction_id());
        if (null != microPay.getPayable()) setPayable(microPay.getPayable());
        setActivity(microPay.getActivity());
        setPaidOut(microPay.getPaidout());
        setCommissin(microPay.getCommission());
        setSign(microPay.getSign());
    }
}
